package com.symc.sims0213xfw.service.impl;

import com.symc.sims0213xfw.bean.Student;
import com.symc.sims0213xfw.dao.StudentDao;
import com.symc.sims0213xfw.dao.StudentViewDao;
import com.symc.sims0213xfw.dao.impl.StudentDaoImpl;
import com.symc.sims0213xfw.dao.impl.StudentViewDaoImpl;
import com.symc.sims0213xfw.service.OperationStatusResult;
import com.symc.sims0213xfw.service.StudentService;
import com.symc.sims0213xfw.service.StudentViewService;

/**
 * @Author: 凤文  沈阳医学院2019级医学信息工程 0213
 * @CreateTime: 2022/01/22 20:45
 * @Description:
 */
public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao = new StudentDaoImpl();
    private StudentViewDao studentViewDao = new StudentViewDaoImpl();
    private StudentViewService studentViewService =
            new StudentViewServiceImpl();

    /**
     * 用户可见，删除，返回各种状态
     * 用户不可见，返回不存在
     *
     * @param id
     * @return
     */
    @Override
    public Long deleteStudent(String id) {

        if (studentViewService.getStudentView(id) == null) {
            return OperationStatusResult.NOT_EXIST;//ok
        } else {
            if (studentDao.setStudentIsVisible(id, NOT_VISIBLE) > 0) {
                return OperationStatusResult.SUCCESS;
            } else {
                return OperationStatusResult.DEFEAT;
            }
        }

    }

    /**
     * 如果用户可见，不能添加，返回存在
     * 如果库中有且用户不可见，执行修改，改为可见，返回各种状态。。。
     * 如果库中没有，添加数据，返回各种状态。。。
     *
     * @param id
     * @param name
     * @param classId
     * @return
     */
    @Override
    public Long addStudent(String id, String name, String classId) {

        //对于用户输入的检测
        if (id.length() > Student.ID_LENGTH) {
            return OperationStatusResult.ID_LENGTH_OUT_OF_BOUNDS;
        }

        if (name.length() > Student.NAME_LENGTH) {
            return OperationStatusResult.NAME_LENGTH_OUT_OF_BOUNDS;
        }

        if (classId.length() > Student.CID_LENGTH) {
            return OperationStatusResult.CLASS_ID_LENGTH_OUT_OF_BOUNDS;
        }

        if (new StudentViewServiceImpl().getStudentView(id) != null) {//用户可见
            return OperationStatusResult.EXIST;
        } else if (studentViewDao.queryStudentView(id) == null) {//库中没有
            if (studentDao.addStudent(id, name, classId, 1) > 0) {
                return OperationStatusResult.SUCCESS;
            } else {
                return OperationStatusResult.DEFEAT;
            }
        } else {//库中有，用户不可见
            if (studentDao.updateStudent(id, name, classId, 1) > 0) {
                return OperationStatusResult.SUCCESS;
            } else {
                return OperationStatusResult.DEFEAT;
            }
        }
    }

    @Override
    public Long updateStudent() {
        return null;
    }

}
